<?php
namespace app\api\model;

use \think\Model;

class DidaApi extends Model
{
    private $baseUrl        = "http://api.didatravel.com/api/";
//    测试账号
//    private $TestClientID   = "kbxcs2019";
//    private $LincenseKey    = "kbxcs2019";
//    生产账号
    private $TestClientID   = "szkbx2017";
    private $LincenseKey    = "kbxcs2019";


    public function post_curl($url,$content=[],$gzip=false){
        $url = $url.'?$format=json';
        $content['Header'] = [
            'ClientID'=>$this->TestClientID,
            'LicenseKey'=>$this->LincenseKey,
        ];
        $contentString = json_encode($content);
        $ch = curl_init();
        if(substr($url,0,5)=='https'){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
        }

        if($gzip) curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // 读取压缩文件

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $contentString);
        $response = curl_exec($ch);
        if($error=curl_error($ch)){
            die($error);
        }
        curl_close($ch);

        $response = json_decode($response,true);
        $response = $this->object_array($response);
        return $response;
    }

    public function object_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        }
        if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }


    //**********静态数据接口***************************************
    //获取国家
    public function GetCountryList(){//美国US     加拿大CA
        $url = $this->baseUrl.'staticdata/GetCountryList';
        $data = $this->post_curl($url);

        dump($data);
        exit;
    }

    //获取城市
    public function GetCityList($code='US'){
        $url = $this->baseUrl.'staticdata/GetCityList';
        $post = [
            "IncludeSubCity"=>true,
            "CountryCode"=>$code,
        ];
        $data = $this->post_curl($url,$post);
        $res = !empty($data['Success']['Cities']) ? $data['Success']['Cities'] : [];

        return $res;
    }

    //获取床型
    public function GetBedTypeList(){
        $url = $this->baseUrl.'staticdata/GetBedTypeList';
        $data = $this->post_curl($url);

        return $data;
    }

    //获取早餐
    public function GetBreakfastTypeList(){
        $url = $this->baseUrl.'staticdata/GetBreakfastTypeList';
        $data = $this->post_curl($url);

        return $data;
    }

    //获取酒店类别
    public function GetPropertyCategoryList(){
        $url = $this->baseUrl.'staticdata/GetPropertyCategoryList';
        $data = $this->post_curl($url);

        return $data;
    }

    //获取餐型
    public function GetMealTypeList(){
        $url = $this->baseUrl.'staticdata/GetMealTypeList';
        $data = $this->post_curl($url);

        return $data;
    }

    //获取酒店详情列表Url
    public function GetStaticInformation(){
        $url = $this->baseUrl.'staticdata/GetStaticInformation';
        $post = [
            'IsGetUrlOnly'=>true,
            "StaticType"=>"HotelSummary",
        ];
        $data = $this->post_curl($url,$post);

        return $data['Url'];
    }


    //**********价格接口***************************************
    //返回客户请求酒店列表中酒店的所有可卖价格计划
    public function getListPriceSearch()
    {
        $url = $this->baseUrl.'rate/pricesearch';
    }
    //缓存价格查询
    public function priceSearch($HotelArr,$CheckInDate,$CheckOutDate){
        //查询多天数据时，只要有一天没有报价，就不会返回，所以应该一天天进行查询
        $url = $this->baseUrl.'rate/pricesearch';
        $post = [];
        $post['CheckInDate'] = $CheckInDate;//到店日期
        $post['CheckOutDate'] = $CheckOutDate;//离店日期
        $post['Nationality'] = "CN";//入住人国籍(ISO 代码), 选填，默认为CN
        $post['Currency'] = "CNY";//请求的货币类型，选填， 默认为CNY

        $HotelIDArr = [];
        foreach ($HotelArr as $k=>$v) {
            $HotelIDArr[] = intval($v);
        }
        $post['HotelIDList'] = $HotelIDArr;//酒店ID组
        $post['LowestPriceOnly'] = true;//是否获取最低报价

        $data = $this->post_curl($url,$post,true);
        $res = !empty($data['Success']['PriceDetails']['HotelList']) ? $data['Success']['PriceDetails']['HotelList'] : [];

        return $res;
    }

    //单酒店实时查询价格
    /***
     * @param $CheckInDate      2019-11-05
     * @param $CheckOutDate     2019-11-10
     * @param $HotelID      13892
     * @param $RoomCount        1
     * @param $AdultCount       2
     * @param $ChildCount       2
     * @param  $ChildAgeDetails     [12,10]
     */
    public function realTimePrice($CheckInDate,$CheckOutDate,$HotelID,$RoomCount,$AdultCount,$ChildCount,$ChildAgeDetails){
        $url = $this->baseUrl.'rate/pricesearch';
        $post = [];
        $post['CheckInDate'] = $CheckInDate;//到店日期
        $post['CheckOutDate'] = $CheckOutDate;//离店日期
        $post['Nationality'] = "CN";//入住人国籍(ISO 代码), 选填，默认为CN
        $post['Currency'] = "CNY";//请求的货币类型，选填， 默认为CNY
        $post['HotelIDList'] = [intval($HotelID)];//酒店ID
        $post['IsRealTime'] = [
            'Value'=>true,
            'RoomCount'=>intval($RoomCount),
        ];//设置是否获取实时报价，及房间数量
        $post['RealTimeOccupancy'] = [
            'AdultCount'=>intval($AdultCount),
            'ChildCount'=>intval($ChildCount),
            'ChildAgeDetails'=>$ChildAgeDetails,
        ];//入住人信息   获取实时报价时使用
//        return $post;
        $data = $this->post_curl($url,$post,true);
        $resData = !empty($data['Success']['PriceDetails']['HotelList'][0])?$data['Success']['PriceDetails']['HotelList'][0]:[];

        return $resData;
    }




}